<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: lh
 * Time: 2020/07/13
 * Desc: 积分商城
 */
class Controller_Pc_Api_V2_Integral_Mall extends Controller_Pc_Api_V2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Integral_Mall
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Integral_Mall
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('integral_mall', $this->request_params);

        // 服务
        $this->service = new Service_Api_Integral_Mall();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 获取产品属性分类
    public function action_attr()
    {
        try {
            $this->result['data']['attr'] = $this->service->attr();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 兑换列表
    public function action_query()
    {
        try {
            $this->validation->query()->check();
            $this->request_params['member_id'] = $this->mid;
            $this->result['data']['list'] = $this->service->query($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 积分商城产品详情页
    public function action_info()
    {
        try {
            $this->validation->info()->check();

            $this->result['data']['detail'] = $this->service->info($this->request_params['id'], $this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 默认地址
    public function action_get_default_address()
    {
        try {
            $this->validation->get_default_address()->check();

            $this->result['data'] = $this->service->get_default_address($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 兑换产品
    public function action_exchange()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->exchange()->check();

            $this->result['data'] = $this->service->exchange($this->mid, $this->request_params);

            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());

            $db->rollback();
        }
    }

    // 获取积分商城订单列表
    public function action_get_order_list()
    {
        try {
            $this->validation->get_order_list()->check();

            $this->result['data']['list'] = $this->service->get_order_list($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * 订单详细
     */
    public function action_get_order_info()
    {
        try {
            $this->validation->get_order_info()->check();

            $this->result['data'] = $this->service->get_order_info($this->mid, $this->request_params['id']);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }
}